package com.acs.player;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.acs.player.constants.PlayerConst;
import com.acs.player.entities.Music;
import com.acs.player.media.Player;
import com.acs.player.media.RetrieveData;
import com.acs.player.views.simple.TrackAdapter;

public class SearchActivity extends Activity {

    private ArrayAdapter<String> 	searchAdapter;
    private TrackAdapter			trackAdapter ; 
    private RetrieveData 			retrieveData ; 
    private AutoCompleteTextView 	acTextView	;
    private List<String>			artistNames ;  
    private Menu 					menu;
    
    
    @Override
    protected void onNewIntent(Intent intent) {
    	// TODO Auto-generated method stub
    	
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);
        
//        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, employees);
//        ListView employeeList = (ListView) findViewById(R.id.list);
//        employeeList.setAdapter(adapter);
        getArtistNames(); 
        AutoCompleteInit();
        
      //CREATE MENU
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.search_container);       
        menu = new Menu();
        LinearLayout linearLayout = menu.createMenu(this);
        relativeLayout.addView(linearLayout);

    }
    
    private void getArtistNames() {

    	artistNames = new ArrayList<String>();
		List<Music> allTrackList = retrieveData.getRetrieveData(this).getAllTracksList(); 
    	for (Music music : allTrackList) 
    	{
			artistNames.add(music.getArtiste().getNom());
		}
    
    }

	private void startAdapter() {
		searchAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,artistNames){
			@Override
			public int getPosition(String item) {
				return artistNames.indexOf(item);
			}			
		};
//		searchAdapter = new SearchAdapter(this,android.R.layout.simple_dropdown_item_1line,artistNames);
	}


	//Evenement pour le bouton Chercher
    public void onClick(View v){
    	//methode 
//    	TextView textResult = (TextView) findViewById(R.id.search_autoComplete);
    	int t = searchAdapter.getPosition(acTextView.getText().toString());
    	if(v.getId() == R.id.searchButton)
    	{
    		ListView resultList = (ListView) findViewById(R.id.listViewResult);
    		resultList.setAdapter(trackAdapter = new TrackAdapter(this, android.R.layout.simple_list_item_1,retrieveData.getRetrieveData(this).getTrackByTitleID(t)));
    	}
    	
    }
    
    public void AutoCompleteInit()
    {
    	startAdapter();
    	acTextView = new AutoCompleteTextView(this);
		acTextView = (AutoCompleteTextView) findViewById(R.id.search_autoComplete);
		acTextView.setThreshold(1);
		acTextView.setAdapter(searchAdapter);		
    }
    
	public void goToPlayerActivity(int trackIndex) {
			
		Player player = Player.getPlayer();
	
		RetrieveData retrieveData = RetrieveData.getRetrieveData(this);
		List<Music> tracksList = retrieveData.getTrackByTitleID(trackIndex);
		if(!player.getTrackListsToPlay().equals(tracksList)){
			player.setTrackListsToPlay(tracksList);
		}
		
		Intent intent = new Intent(SearchActivity.this, PlayerActivity.class); 
		intent.putExtra(PlayerConst.CURRENT_TRACK_INDEX_KEY, trackIndex);
		intent.putExtra(PlayerConst.PREVIOUS_ACTIVITY_KEY, PlayerConst.SEARCH_VIEW);
		
		startActivity(intent);	
	}
	
    public void click(View view){
    	menu.click(view);
    }
    
    
    
}
